package order;

import java.util.HashMap;

import model.*;

import javax.swing.JOptionPane;

public class Attack extends ActionOrder{

	public Attack(Territory source, Player player, Territory destination, HashMap<TechnologyLevel, Integer> troopAmount){
        super(source, player, destination, troopAmount); 
    }
	
	protected boolean checkOwner(){
		if(source.getOwner() != player){
			JOptionPane.showMessageDialog(null, "You can only use your own territory!");
			 return false;
		}
		if (source.getOwner() != destination.getOwner()) 
			if (source.getOwner().isAlly(destination.getOwner())){
			    JOptionPane.showMessageDialog(null, "You can't attack your ally!");
			    return false;
			}
        else {
            JOptionPane.showMessageDialog(null, "You can only attack an enemy's territory!");
          return false;
        }
	   return true;
    }

    protected boolean checkPath(Map map){
        if (source.checkNeighbor(destination)) return true;
        else {
        	JOptionPane.showMessageDialog(null, "You can only attack a adjacent territory!");
            return false;
        }
    }
    
   public void undoOrder(){
    	for (TechnologyLevel level:TechnologyLevel.values())
     	   for (int j = 0; j < troopAmount.get(level); j++){
     		   source.addTroop(new Troop(level, player));
     	}
    }
    
    public  String printOrder(){
    	StringBuffer sentence = new StringBuffer();
    	sentence.append("Attack ");
    	sentence.append(destination.getLocation() + "by ");
    	for (TechnologyLevel level:TechnologyLevel.values())
    		if (troopAmount.get(level) > 0){
    		sentence.append(troopAmount.get(level));
    		sentence.append(" " + level + ", ");
    	}
    	sentence.append(" from " + source.getLocation());
        return sentence.toString();
    }

	@Override
	public void executeOrder() {
		for (TechnologyLevel level:TechnologyLevel.values())
	    	   for (int j = 0; j < troopAmount.get(level); j++){
	    		   source.removeTroop(level, player);
	    	   }
	}

}
